package com.erpanderp.wam.usm.view.user;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.usm.model.entity.user.BirthDayWishes;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.BirthDayWishesDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMHostManager;

@ManagedBean
@ViewScoped
public class BirthdayWishesBean implements Serializable{

	
	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private TimeZone timeZone = TimeZone.getDefault();
	
	private List<BirthDayWishes> birthDayWishesList = new ArrayList<BirthDayWishes>();
	
	private BirthDayWishes birthDayWishes=new BirthDayWishes();
	private Employee employee=new Employee();
	

	private BirthDayWishesDAO birthDayWishesDAO=new BirthDayWishesDAO();
	
	
	private boolean FORM_VALID;
	private String EMAIL_MESSAGE;
	private EmailUtil emailUtil;
	private int sNo=1;
	

	public BirthdayWishesBean() {
		
		
		birthDayWishesList = birthDayWishesDAO.getAllEmpBdaysByDate();
		
		for(BirthDayWishes bWishes:birthDayWishesList){
			bWishes.setsNo(sNo);
			sNo++;
		}
		
	}
	
	public void birthdayListener(ActionEvent e)  {
		 
		 Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		String employeeCode = params.get("birthDayId");		
		 
		 long id = Long.parseLong(employeeCode);

		 birthDayWishes=birthDayWishesDAO.getBirthDayWishesById(id);
		
  }
	
	public String saveBirthDayGreetingsAction()  {
		
		 validateForm();
		   if(FORM_VALID){
		
			   birthDayWishes.setDateModified(new Date());
			   birthDayWishes.setModifiedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			  
			   birthDayWishesDAO.saveOrUpdateBirthDayWishes(birthDayWishes);
			   
			   System.out.println("AT CALLER METHOD: " + sendMessage());  //*******DONT REMOVE*******
			   
			   return "birthdaywishes";
		    }else{		    	
		    	RequestContext.getCurrentInstance().execute("confirmationDialog.show();");
		    	return null;
		    }
	}
	
	//INPUTS VALIDATIONS
		private void validateForm() {
			FORM_VALID = true;

				if (ValidationUtil.isEmpty(birthDayWishes.getWishMessage(), "Greeting Message", null,
						FacesContext.getCurrentInstance()) != null) {
					FORM_VALID = false;
				}
		}
		
		//SENDING EMAIL 
		public String sendMessage(){
			Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
			String domainName = context.getInitParameter("domainName");
			String messageString ="";
			
			
			String employeeMail =null;
			
			
			emailUtil = new EmailUtil();
			if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
				System.out.println("PRODUCTION");
				employeeMail = birthDayWishes.getEmployee().getEmail();
				
			} else {
				System.out.println("DEVELOPMENT");
				employeeMail = hostsMap.get(CommonLookup.DEV_EMAIL);	
			}	
			
			String[] to={employeeMail};
			
			messageString = "<font face='comic sans MS, new york, times' size='2' color='#2FC89C'><b>Dear " + birthDayWishes.getEmployee().getFirstName() + " " + birthDayWishes.getEmployee().getLastName() + "</b></font>" +
								",<br/><br/>" +									
						
			        
					"<font face='comic sans MS, new york, times' size='2' color='#F54E94'><b>"+birthDayWishes.getWishMessage()+"</b></font><br/><br/>"+
			        
					
					
					"<font face='comic sans MS, new york, times' size='2' color='#2FC89C'><b>Thanks & Regards, <br />ERPandERP India Pvt Ltd</b></font>";
				
				emailUtil = new EmailUtil();
				emailUtil.setMessage(messageString);
				emailUtil.setRecieverAddresses(to);
				
				emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
				
				emailUtil.setSubject("** MANY MANY HAPPY RETURNS OF THE DAY **");
				
			
				
			
			
			SwingWorker worker = new SwingWorker<String, Object>() {
				@Override
				protected String doInBackground() throws Exception {
					EMAIL_MESSAGE = emailUtil.processAndSendEmail();
					return null;
				}

				@Override
				protected void done() {
						System.out.println("DONE " + EMAIL_MESSAGE);		
				}
			};
			worker.execute();
			System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
			return EMAIL_MESSAGE;
		}


	public WAMUser getWamUser() {
		return wamUser;
	}


	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}


	public TimeZone getTimeZone() {
		return timeZone;
	}


	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}


	public ServletContext getContext() {
		return context;
	}


	public void setContext(ServletContext context) {
		this.context = context;
	}

	public BirthDayWishes getBirthDayWishes() {
		return birthDayWishes;
	}


	public void setBirthDayWishes(BirthDayWishes birthDayWishes) {
		this.birthDayWishes = birthDayWishes;
	}

	public int getsNo() {
		return sNo;
	}


	public void setsNo(int sNo) {
		this.sNo = sNo;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public List<BirthDayWishes> getBirthDayWishesList() {
		return birthDayWishesList;
	}

	public void setBirthDayWishesList(List<BirthDayWishes> birthDayWishesList) {
		this.birthDayWishesList = birthDayWishesList;
	}

	public BirthDayWishesDAO getBirthDayWishesDAO() {
		return birthDayWishesDAO;
	}

	public void setBirthDayWishesDAO(BirthDayWishesDAO birthDayWishesDAO) {
		this.birthDayWishesDAO = birthDayWishesDAO;
	}

}
